/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormSession.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "forms/FormSession.h"
#include "SCFormManager.h"
#include "SCNavigator.h"
#include "SCTranslator.h"
#include "models/SCSession.h"
#include "models/SCHelpers.h"
#include "SCDatabase.h"
#include "models/SCDataModification.h"

FormSession::FormSession()
{
}

FormSession::~FormSession()
{
}


bool
FormSession::Initialize()
{
	Construct(L"IDF_SESSION");
	return true;
}

result
FormSession::OnInitializing(void)
{
	result r = E_SUCCESS;

	//Set Session
	this->pSessionsList = SCSession::GetAllSessions();

	//set MainMenu
	SCNavigator::SetMainMenu(this, GetTab(), SCFormManager::IDF_SESSIONS);

	this->pSessionsListView = static_cast<ListView*>(GetControl("IDC_LISTVIEW_SESSIONS"));
	this->pSessionsListView->SetItemProvider(*this);
	this->pSessionsListView->AddListViewItemEventListener(*this);
	this->pSessionsListView->SetTextOfEmptyList(SCTranslator::GetText("IDS_SESSION_EMPTYLIST"));
	this->pSessionsListView->Draw();

	//Footer
	Footer* pFooter = GetFooter();
	pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
	pFooter->AddActionEventListener(*this);

	FooterItem* pRemoveItem = new FooterItem();
	pRemoveItem->Construct(IDA_SESSION_REMOVE);
	pRemoveItem->SetText(SCTranslator::GetText("IDS_GENERAL_REMOVE"));

	pFooter->AddItem(*pRemoveItem);

	delete pRemoveItem;

	return r;
}

result
FormSession::OnTerminating(void)
{
	result r = E_SUCCESS;
	return r;
}

void
FormSession::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch (actionId)
	{
		case IDA_SESSION_INFO:
			SCFormManager::SwitchToForm(SCFormManager::IDF_SESSIONS_INFO, false, null, SCFormManager::SLIDE_LEFT);
			break;
		case IDA_SESSION_REMOVE:
			SCFormManager::SwitchToForm(SCFormManager::IDF_SESSIONS_DELETE, false, null, SCFormManager::NO_SLIDE);
			break;
		case SCFormManager::IDF_ALARM:
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM, false, null, SCFormManager::NO_SLIDE);
			break;
		case SCFormManager::IDF_SETTINGS:
			SCFormManager::SwitchToForm(SCFormManager::IDF_SETTINGS, false, null, SCFormManager::NO_SLIDE);
			break;
		case SCFormManager::IDF_CATCH_SLEEP:
			SCFormManager::SwitchToForm(SCFormManager::IDF_CATCH_SLEEP, false, null, SCFormManager::NO_SLIDE);
			break;
		case SCFormManager::IDF_STATISTICS:
			SCFormManager::SwitchToForm(SCFormManager::IDF_STATISTICS, false, null, SCFormManager::NO_SLIDE);
			break;
		default:
			break;
	}
}

int
FormSession::GetFormId()
{
	return SCFormManager::IDF_SESSIONS;
}

void
FormSession::OnListViewContextItemStateChanged (Osp::Ui::Controls::ListView &listView, int index, int elementId, Osp::Ui::Controls::ListContextItemStatus status)
{
}

void
FormSession::OnListViewItemLongPressed (Osp::Ui::Controls::ListView &listView, int index, int elementId, bool &invokeListViewItemCallback)
{
}

void
FormSession::OnListViewItemStateChanged (Osp::Ui::Controls::ListView &listView, int index, int elementId, Osp::Ui::Controls::ListItemStatus status)
{
	ArrayList* pList = new ArrayList();
	pList->Add(*(this->pSessionsList->GetAt(index)));
	SCFormManager::SwitchToForm(SCFormManager::IDF_SESSIONS_INFO, false, pList, SCFormManager::SLIDE_LEFT);
	pList->RemoveAll();
	delete pList;
}

void
FormSession::OnListViewItemSwept (Osp::Ui::Controls::ListView &listView, int index, Osp::Ui::Controls::SweepDirection direction)
{
}

Osp::Ui::Controls::ListItemBase*
FormSession::CreateItem(int index, int itemWidth)
{
	CustomItem* pCustomItem = NULL;
	IEnumerator *pEnum = NULL;
	SCSession *pSession;
	int titleSize = 20;
	Color *pColor;
	pColor = new Color(193, 228, 247);

	if(this->pSessionsList->GetCount() > 0)
	{
		pEnum = this->pSessionsList->GetEnumeratorN();
		pSession = static_cast<SCSession*>(this->pSessionsList->GetAt(index));

		pCustomItem = new CustomItem();
		pCustomItem->Construct(Dimension(itemWidth,150), LIST_ANNEX_STYLE_NORMAL);

		// Title,from - to
		String sampleCountStr;
		sampleCountStr.Append(pSession->GetSamplesCount());
		String dateFrom = SCHelpers::ConvertDateTimeToString(pSession->GetStartDate(), "dd.MM.");
		String dateTo = SCHelpers::ConvertDateTimeToString(pSession->GetStopDate(), "dd.MM.");
		String titleStr = SCTranslator::GetText("IDS_SESSION_SLEEPRECORD")+ ": " + dateFrom + " - " + dateTo + "," + SCTranslator::GetText("IDS_SESSION_SAMPLESCOUNT") + ": " + sampleCountStr;
		pCustomItem->AddElement(Rectangle(0,0,itemWidth,20), 0, titleStr , titleSize, *pColor, Color::COLOR_WHITE, *pColor, true);

		// Set BackgroundImg
		Image* pImage = new Image();
		Bitmap* pGraph;
		pImage->Construct();
		String dstPath = "/Home/Sessions/Graphs/";
		dstPath.Append(pSession->GetId());
		dstPath.Append(".png");
		if(File::IsFileExist(dstPath))
		{
			pGraph = pImage->DecodeN(dstPath, BITMAP_PIXEL_FORMAT_RGB565);
		} else {
			pGraph = pImage->DecodeN(L"/Home/Icons/Commons/sleepGraphNoPhoto.png", BITMAP_PIXEL_FORMAT_RGB565);
		}

		pCustomItem->AddElement(Rectangle(0,20,itemWidth, 130), 1, *pGraph, pGraph, pGraph);

		delete pImage;
		delete pEnum;
		delete pGraph;
	} else {
		this->pSessionsListView->SetTextOfEmptyList(SCTranslator::GetText("IDS_ALARMS_NO_ALARMS"));
	}

	delete pColor;

	return pCustomItem;
}

bool
FormSession::DeleteItem(int index, Osp::Ui::Controls::ListItemBase* pItem, int itemWidth)
{
	delete pItem;
	pItem = NULL;
	return true;
}

int
FormSession::GetItemCount(void)
{
	if(this->pSessionsList != NULL)
	{
		return this->pSessionsList->GetCount();
	} else {
		return 0;
	}
}

void
FormSession::LoadAllSessions()
{
	this->pSessionsList = SCSession::GetAllSessions();
}

void
FormSession::Update()
{
	this->LoadAllSessions();

	// reload alarmListView
	if(SCDatabase::GetReloadSessions())
	{
		ArrayList* pDataModificationList = SCDatabase::GetDataModificationList();
		IEnumerator* pEnum = pDataModificationList->GetEnumeratorN();
		SCDataModification* pDataModification;
		ArrayList* pDeletedItems = new ArrayList();

		while(pEnum->MoveNext() == E_SUCCESS)
		{
			pDataModification = static_cast<SCDataModification*>(pEnum->GetCurrent());

			if(pDataModification->GetFormId() == SCFormManager::IDF_SESSIONS)
			{
				switch(pDataModification->GetOperationType())
				{
					case SCDatabase::ACTION_ADD:
						this->pSessionsListView->RefreshList(pDataModification->GetItemId(), LIST_REFRESH_TYPE_ITEM_ADD);
						break;
					case SCDatabase::ACTION_MODIFY:
						this->pSessionsListView->RefreshList(pDataModification->GetItemId(), LIST_REFRESH_TYPE_ITEM_MODIFY);
						break;
					case SCDatabase::ACTION_DELETE:
						this->pSessionsListView->RefreshList(pDataModification->GetItemId(), LIST_REFRESH_TYPE_ITEM_REMOVE);
						break;
				}

				pDeletedItems->Add(*pDataModification);
			}
		}

		//delete items
		pDataModificationList->RemoveItems(*pDeletedItems, true);

		SCDatabase::SetReloadSessions(false);

		//dealloc
		pDeletedItems->RemoveAll();
		delete pDeletedItems;
		delete pEnum;
	}

	// scroll up
	if(this->pSessionsList->GetCount() > 0)
	{
		this->pSessionsListView->ScrollToItem(0);
	}

	//Set active tab
	SCNavigator::SetActiveTab(this->GetTab(), SCFormManager::IDF_SESSIONS);
}

void
FormSession::Redraw()
{
	this->Update();
	RequestRedraw(true);
}


